package com.zhj.leetcode;

import java.util.*;

/**
 * 力扣49
 * 字母异位词分组
 * @author zhj
 * @create 2024-02-18-09:50
 */
public class Test49 {

    public static void main(String[] args) {
        String[] strs = new String[]{"ant", "tna", "eat"};
        System.out.println(groupAnagrams(strs));
    }


    public static List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for (String str : strs) {
            char[] strArr = str.toCharArray();
            Arrays.sort(strArr);
            String key = new String(strArr);
            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}
